$.ajax({
    type: 'POST',
    url: '/isLogin',
    async: false,
    data: {},
    dataType: 'json',
    success: function (result) {
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        if (XMLHttpRequest.status==555){
            var layer;
            layui.use(['layer', 'form'], function(){
                layer = layui.layer
                layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                    //do something
                    layer.close(index);
                    top.location.href="/login";
                });
            });
        }else{
            var layer;
            layui.use(['layer', 'form'], function(){
                layer = layui.layer
                layer.alert('页面加载失败',{icon:7}, function(index){
                    //do something
                    layer.close(index);
                });
            });
        }
    },
});
layui.use('table', function(){
    var table = layui.table;

    //方法级渲染
    table.render({
        elem: '#LAY_table_user'
        ,url: "/query/getMerchantMsg2"
        ,cols: [[
            // {field:'id', title: 'ID', width:100, }
            {field:'name', title: '账户名', width:350,}
            ,{field:'code', title: '账户编号', width:250}
            ,{field:'p1', title: '联系人', width:200}
            ,{field:'phone1', title: '联系电话', width:250}
            ,{field:'accesstime', title: '开户时间',width:250}
            ,{field:'state', title: '状态', align: 'center', width:202}
        ]]
        ,id: 'testReload'
        ,page: true
        ,height: 552
        ,response: {
            statusCode: 666 //规定成功的状态码，默认：0
        }
        // ,even:true //每行条纹
        ,limits: [10,15,20,25,30]
        ,limit: 10 //每页默认显示的数量
        ,toolbar:'#toolbarDemo'  //导出打印
        ,parseData:function (res) {
            console.log(res);
            return{
                "code":res.code,
                "msg":res.msg,
                "count":res.data.merchantList.length,
                "data":res.data.merchantList
            };
        }
        , done: function(res, curr, count){
            // $(".layui-table-box").find("[data-field='id']").css("display","none");

            $("[data-field='state']").children().each(function(){
                if($(this).text()=='1'){
                    $(this).text("已启用")
                }else if($(this).text()=='0'){
                    $(this).text("未启用");
                }else if($(this).text()=='2'){
                    $(this).text("已结算");
                }else if($(this).text()=='-1'){
                    $(this).text("已注销");
                }
            });
            $("[data-field='accesstime']").children().each(function(){
                var re = /^[0-9]+.?[0-9]*/;//判断字符串是否为数字//判断正整数/[1−9]+[0−9]∗]∗/
                if (re.test($(this).text())) {
                    $(this).text(formatDate($(this).text()));
                }
            });
        }

    });

    var $ = layui.$, active = {
        reload: function(){
            var demoReload = $('#demoReload');
            //执行重载
            table.reload('testReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    key: {
                        id: demoReload.val()
                    }
                }
            });
        }
    };

    $('.layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //监听行单击事件（单击事件为：rowDouble）
    //其中的user对应的是 table中的lay-filter="user"
    table.on('row(user)', function(obj){
        var data = obj.data;
        var id=data['id'];
        // layer.alert(JSON.stringify(data.id));
        window.location.href="/query/queryPage?id="+id;
        //标注选中样式
        obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
    });

});

function formatDate(timestamp) {
    if (timestamp) {
        var time = new Date(timestamp*1000);
        var y = time.getFullYear();
        var M = time.getMonth() + 1;
        var d = time.getDate();
        var h = time.getHours();
        var m = time.getMinutes();
        var s = time.getSeconds();
        return y + '-' + addZero(M) + '-' + addZero(d) + ' ' + addZero(h) + ':' + addZero(m) + ':' + addZero(s);
    } else {
        return '';
    }
}
function addZero(m) {
    return m < 10 ? '0' + m : m;
}
